﻿using System;
using System.Collections.Generic;

namespace Data
{
    public class IntervalSegmentation
    {
        public static List<(int start, int end)> SegmentInterval(int total, int n)
        {
            var segments = new List<(int start, int end)>();
            var segmentSize = n;
            var currentStart = 1;

            var avgRange = total % n == 0 ? total / n : total / n + 1;

            for (var i = 0; i < avgRange; i++)
                if (i != avgRange - 1)
                {
                    var currentEnd = currentStart + segmentSize - 1;
                    segments.Add((currentStart, currentEnd));
                    currentStart = currentEnd + 1;
                } else
                {
                    segments.Add((currentStart, total));
                }

            return segments;
        }

        // 寻找包含指定值的区间
        public static ValueTuple<int, int> FindIntervalContainingValue(List<(int start, int end)> intervals, int value)
        {
            foreach (var interval in intervals)
                if (value >= interval.start && value <= interval.end)
                    return interval;
            return (0, 0);
        }
    }
}